package ms.shop.controller;

import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.string.StringUtil;
import ms.shop.bean.AttrParamBean;
import ms.shop.bean.CommonQueryParamBean;
import ms.shop.bean.SkuParamBean;
import ms.shop.config.ResponseCode;
import ms.shop.entity.Sku;
import ms.shop.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Project Name: ms
 * File Name: SkuController
 * Package Name: ms.shop.controller
 * Date: 2018/6/4  10:29
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@RestController
@RequestMapping("/sku")
public class SkuController {

    @Autowired
    private SkuService skuService;

    /**
     * 分页查询
     * @param commonQueryParamBean
     * @return
     */
    @PostMapping("/query")
    public Page<Sku> query(@RequestBody CommonQueryParamBean commonQueryParamBean) {
        if(StringUtil.isEmpty(commonQueryParamBean.getPageNo())) {
            commonQueryParamBean.setPageNo(1);
        }
        if(StringUtil.isEmpty(commonQueryParamBean.getPageSize())) {
            commonQueryParamBean.setPageSize(10);
        }

        Page<Sku> page = skuService.query(commonQueryParamBean);
        return page;
    }

    /**
     * 新增
     * @param skuParamBean
     * @return
     */
    @PostMapping("/create")
    public void create(@RequestBody SkuParamBean skuParamBean) {
        //参数校验
        checkParam(skuParamBean);

        skuService.create(skuParamBean);
    }

    /**
     * 修改
     * @param skuParamBean
     * @return
     */
    @PostMapping("/update")
    public void update(@RequestBody SkuParamBean skuParamBean) {
        //参数校验
        checkParam(skuParamBean);

        skuService.update(skuParamBean);
    }


    /**
     * 启用
     * @param id
     * @return
     */
    @PostMapping("/use/{id}")
    public void use(@PathVariable("id") Long id) {
        skuService.use(id);
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @PostMapping("/cancel/{id}")
    public void cancel(@PathVariable("id") Long id) {
        skuService.cancel(id);
    }
    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("/detail/{id}")
    public Sku detail(@PathVariable("id") Long id) {
        return skuService.detail(id);
    }

    /**
     * 清除缓存
     * @return
     */
    @PostMapping("/clear")
    public void clear() {
        skuService.clear();
    }

    /**
     * 校验新增和修改参数
     * @param skuParamBean
     */
    private void checkParam(SkuParamBean skuParamBean) {
        if(StringUtil.isEmpty(skuParamBean.getGoodId())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "商品信息不能为空");
        }
        if(StringUtil.isEmpty(skuParamBean.getName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "名称不能为空");
        }
        if(StringUtil.isEmpty(skuParamBean.getPrice())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "价格不能为空");
        }
        if(StringUtil.isEmpty(skuParamBean.getRealPrice())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "真实价格不能为空");
        }
        if(StringUtil.isEmpty(skuParamBean.getImgs())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "图片不能为空");
        }
        if(StringUtil.isEmpty(skuParamBean.getAttrId())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "属性不能为空");
        }
        if(null == skuParamBean.getNum() || skuParamBean.getNum() == 0) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "数量不能为空");
        }
    }
}
